package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjecyExceyListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Wrapper;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-06-22
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService eduSubjectService) {
        try {
            InputStream in =  file.getInputStream();

            EasyExcel.read(in, SubjectData.class,new SubjecyExceyListener(eduSubjectService)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    //！！！！！！！！！！！！！！！！思考代码如何优化
    @Override
    public List<OneSubject> getAllOneTwoSubject() {
        //1.查询所有一级
        QueryWrapper<EduSubject> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_id","0");
        List<EduSubject> oneeduSubjects = baseMapper.selectList(wrapper);

        //2.查询所有二级
        QueryWrapper<EduSubject> wrapper2 = new QueryWrapper<>();
        wrapper2.ne("parent_id","0");
        List<EduSubject> twoeduSubjects = baseMapper.selectList(wrapper2);
//        System.out.println(twoeduSubjects);

        //创建list集合，用于储存最终封装的数据
        ArrayList<OneSubject> finalSubjectList = new ArrayList<>();

        //3.封装一级
        //将查询出来的list 遍历，得到每个一级对象，用属性封装我们自己的 一级分类对象
        for (int i = 0; i < oneeduSubjects.size(); i++) {
            EduSubject eduSubject = oneeduSubjects.get(i);//每个对象

            //把eduSubject里面的值取出来放到oneSbject
            OneSubject oneSubject = new OneSubject();
//            oneSubject.setTitle(eduSubject.getTitle());
//            oneSubject.setId(eduSubject.getId());
            //化简。属性值相同
            BeanUtils.copyProperties(eduSubject,oneSubject);

//            finalSubjectList.add(oneSubject);  //得到所有一级分类
            ArrayList<TwoSubject> finalTwoSubjectList = new ArrayList<>();
            for (int m = 0; m < twoeduSubjects.size(); m++) {
                EduSubject teduSubject1 = twoeduSubjects.get(m);
                if(teduSubject1.getParentId().equals(eduSubject.getId()) )
                {
                    TwoSubject twoSubject = new TwoSubject();
                    BeanUtils.copyProperties(teduSubject1,twoSubject);
                    finalTwoSubjectList.add(twoSubject);
                }
            }

            oneSubject.setChildren(finalTwoSubjectList);
            finalSubjectList.add(oneSubject);  //得到所有一级分类
        }
        //封装到要求的list集合里面 list<Onesubject> finalSubjectList


        //我的思路
//        for (int i = 0; i < twoeduSubjects.size(); i++) {
//            TwoSubject twoSubject = new TwoSubject();
//            EduSubject eduSubject = twoeduSubjects.get(i);
//            BeanUtils.copyProperties(eduSubject,twoSubject);
//            ArrayList<TwoSubject> twoSubjects = new ArrayList<>();
//            for (int i1 = 0; i1 < finalSubjectList.size(); i1++) {
//                String id = twoSubject.getId();
//                if(id == finalSubjectList.get(i1).getId())
//                {
//                    finalSubjectList.get(i1)
//                    twoSubjects.add(twoSubject);
//                }
//                finalSubjectList.get(i1).setChildren();
//            }
//            finalSubjectList.get(i)
//        }


        //4.封装二级

        return finalSubjectList;
    }
}
